home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / bison.lha / bison++-1.04 / lex.c < prev    next >
C/C++ Source or Header  |  1989-07-05  |  9KB  |  470 lines

  1. /* Token-reader for Bison's input parser,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* 
  22.    lex() is the entry point.  It is called from reader.c.
  23.    It returns one of the token-type codes defined in lex.h.
  24.    When an identifier is seen, the code IDENTIFIER is returned
  25.    and the name is looked up in the symbol table using symtab.c;
  26.    symval is set to a pointer to the entry found.  */
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include "files.h"
  31. #include "symtab.h"
  32. #include "lex.h"
  33.  
  34.  
  35. extern int lineno;
  36. extern int translations;
  37.  
  38. int parse_percent_token();
  39.  
  40. extern void fatals();
  41. extern void fatal();
  42.  
  43. char token_buffer[MAXTOKEN + 1];
  44. bucket *symval;
  45. int numval;
  46.  
  47. static int unlexed;        /* these two describe a token to be reread */
  48. static bucket *unlexed_symval;    /* by the next call to lex */
  49.  
  50.  
  51. void
  52. init_lex()
  53. {
  54.   unlexed = -1;
  55. }
  56.  
  57.  
  58.  
  59. int
  60. skip_white_space()
  61. {
  62.   register int c;
  63.   register int inside;
  64.  
  65.   c = getc(finput);
  66.  
  67.   for (;;)
  68.     {
  69.       switch (c)
  70.     {
  71.     case '/':
  72.       c = getc(finput);
  73.       if (c != '*')
  74.         fatals("unexpected '/%c' found",c);
  75.  
  76.       c = getc(finput);
  77.  
  78.       inside = 1;
  79.       while (inside)
  80.         {
  81.           if (c == '*')
  82.         {
  83.           while (c == '*')
  84.             c = getc(finput);
  85.  
  86.           if (c == '/')
  87.             {
  88.               inside = 0;
  89.               c = getc(finput);
  90.             }
  91.         }
  92.           else if (c == '\n')
  93.         {
  94.           lineno++;
  95.           c = getc(finput);
  96.         }
  97.           else if (c == EOF)
  98.         fatal("unterminated comment");
  99.           else
  100.         c = getc(finput);
  101.         }
  102.  
  103.       break;
  104.  
  105.     case '\n':
  106.       lineno++;
  107.  
  108.     case ' ':
  109.     case '\t':
  110.     case '\f':
  111.       c = getc(finput);
  112.       break;
  113.  
  114.     default:
  115.       return (c);
  116.     }
  117.     }
  118. }
  119.  
  120.  
  121. void
  122. unlex(token)
  123. int token;
  124. {
  125.   unlexed = token;
  126.   unlexed_symval = symval;
  127. }
  128.  
  129.  
  130.  
  131. int
  132. lex()
  133. {
  134.   register int c;
  135.   register char *p;
  136.  
  137.   if (unlexed >= 0)
  138.     {
  139.       symval = unlexed_symval;
  140.       c = unlexed;
  141.       unlexed = -1;
  142.       return (c);
  143.     }
  144.  
  145.   c = skip_white_space();
  146.  
  147.   switch (c)
  148.     {
  149.     case EOF:
  150.       return (ENDFILE);
  151.  
  152.     case 'A':  case 'B':  case 'C':  case 'D':  case 'E':
  153.     case 'F':  case 'G':  case 'H':  case 'I':  case 'J':
  154.     case 'K':  case 'L':  case 'M':  case 'N':  case 'O':
  155.     case 'P':  case 'Q':  case 'R':  case 'S':  case 'T':
  156.     case 'U':  case 'V':  case 'W':  case 'X':  case 'Y':
  157.     case 'Z':
  158.     case 'a':  case 'b':  case 'c':  case 'd':  case 'e':
  159.     case 'f':  case 'g':  case 'h':  case 'i':  case 'j':
  160.     case 'k':  case 'l':  case 'm':  case 'n':  case 'o':
  161.     case 'p':  case 'q':  case 'r':  case 's':  case 't':
  162.     case 'u':  case 'v':  case 'w':  case 'x':  case 'y':
  163.     case 'z':
  164.     case '.':  case '_':
  165.       p = token_buffer;
  166.       while (isalnum(c) || c == '_' || c == '.')
  167.     {
  168.       if (p < token_buffer + MAXTOKEN)
  169.         *p++ = c;
  170.       c = getc(finput);
  171.     }
  172.  
  173.       *p = 0;
  174.       ungetc(c, finput);
  175.       symval = getsym(token_buffer);
  176.       return (IDENTIFIER);
  177.  
  178.     case '0':  case '1':  case '2':  case '3':  case '4':
  179.     case '5':  case '6':  case '7':  case '8':  case '9':
  180.       {
  181.     numval = 0;
  182.  
  183.     while (isdigit(c))
  184.       {
  185.         numval = numval*10 + c - '0';
  186.         c = getc(finput);
  187.       }
  188.     ungetc(c, finput);
  189.     return (NUMBER);
  190.       }
  191.  
  192.     case '\'':
  193.       translations = -1;
  194.  
  195.       /* parse the literal token and compute character code in  code  */
  196.  
  197.       c = getc(finput);
  198.       {
  199.     register int code = 0;
  200.  
  201.     if (c == '\\')
  202.       {
  203.         c = getc(finput);
  204.  
  205.         if (c <= '7' && c >= '0')
  206.           {
  207.         while (c <= '7' && c >= '0')
  208.           {
  209.             code = (code * 8) + (c - '0');
  210.             c = getc(finput);
  211.           }
  212.         if (code >= 256 || code < 0)
  213.           fatals("malformatted literal token '\\%03o'",code);
  214.           }
  215.         else
  216.           {
  217.         if (c == 't')
  218.           code = '\t';
  219.         else if (c == 'n')
  220.           code = '\n';
  221.         else if (c == 'r')
  222.           code = '\r';
  223.         else if (c == 'f')
  224.           code = '\f';
  225.         else if (c == 'b')
  226.           code = '\b';
  227.         else if (c == 'v')
  228.           code = 013;
  229.         else if (c == 'x')
  230.           {
  231.             while ((c <= '9' && c >= '0')
  232.                || (c >= 'a' && c <= 'z')
  233.                || (c >= 'A' && c <= 'Z'))
  234.               {
  235.             code *= 10;
  236.             if (c <= '9' && c >= '0')
  237.               code += c - '0';
  238.             else if (c >= 'a' && c <= 'z')
  239.               code += c - 'a' + 10;
  240.             else if (c >= 'A' && c <= 'Z')
  241.               code += c - 'A' + 10;
  242.             c = getc(finput);
  243.               }
  244.             if (code >= 256 || code<0)/* JF this said if(c>=128) */
  245.               fatals("malformatted literal token '\\x%x'",code);
  246.           }
  247.         else if (c == '\\')
  248.           code = '\\';
  249.         else if (c == '\'')
  250.           code = '\'';
  251.         else if (c == '\"')    /* JF this is a good idea */
  252.           code = '\"';
  253.         else fatals("invalid literal token '\\%c'",c);
  254.         c = getc(finput);
  255.           }
  256.       }
  257.     else
  258.       {
  259.         code = c;
  260.         c = getc(finput);
  261.       }
  262.     if (c != '\'')
  263.       fatal("multicharacter literal tokens NOT supported");
  264.  
  265.     /* now fill token_buffer with the canonical name for this character
  266.        as a literal token.  Do not use what the user typed,
  267.        so that '\012' and '\n' can be interchangeable.  */
  268.  
  269.     p = token_buffer;
  270.     *p++ = '\'';
  271.     if (code == '\\')
  272.       {
  273.         p = token_buffer + 1;
  274.         *p++ = '\\';
  275.         *p++ = '\\';
  276.       }
  277.     else if (code == '\'')
  278.       {
  279.         p = token_buffer + 1;
  280.         *p++ = '\\';
  281.         *p++ = '\'';
  282.       }
  283.     else if (code >= 040 && code != 0177)
  284.       *p++ = code;
  285.     else if (code == '\t')
  286.       {
  287.         p = token_buffer + 1;
  288.         *p++ = '\\';
  289.         *p++ = 't';
  290.       }
  291.     else if (code == '\n')
  292.       {
  293.         p = token_buffer + 1;
  294.         *p++ = '\\';
  295.         *p++ = 'n';
  296.       }
  297.     else if (code == '\r')
  298.       {
  299.         p = token_buffer + 1;
  300.         *p++ = '\\';
  301.         *p++ = 'r';
  302.       }
  303.     else if (code == '\b')
  304.       {
  305.         p = token_buffer + 1;
  306.         *p++ = '\\';
  307.         *p++ = 'b';
  308.       }
  309.     else if (code == '\f')
  310.       {
  311.         p = token_buffer + 1;
  312.         *p++ = '\\';
  313.         *p++ = 'f';
  314.       }
  315.         else
  316.       {
  317.         *p++ = code / 0100 + '0';
  318.         *p++ = ((code / 010) & 07) + '0';
  319.         *p++ = (code & 07) + '0';
  320.       }
  321.     *p++ = '\'';
  322.     *p = 0;
  323.     symval = getsym(token_buffer);
  324.     symval->class = STOKEN;
  325.     if (! symval->user_token_number)
  326.       symval->user_token_number = code;
  327.     return (IDENTIFIER);
  328.       }
  329.  
  330.     case ',':
  331.       return (COMMA);
  332.  
  333.     case ':':
  334.       return (COLON);
  335.  
  336.     case ';':
  337.       return (SEMICOLON);
  338.  
  339.     case '|':
  340.       return (BAR);
  341.  
  342.     case '{':
  343.       return (LEFT_CURLY);
  344.  
  345.     case '=':
  346.       do
  347.     {
  348.       c = getc(finput);
  349.       if (c == '\n') lineno++;
  350.     }
  351.       while(c==' ' || c=='\n' || c=='\t');
  352.  
  353.       if (c == '{')
  354.           return(LEFT_CURLY);
  355.       else
  356.     {
  357.       ungetc(c, finput);
  358.       return(ILLEGAL);
  359.     }
  360.  
  361.     case '<':
  362.       p = token_buffer;
  363.       c = getc(finput);
  364.       while (c != '>')
  365.     {
  366.       if (c == '\n' || c == EOF)
  367.         fatal("unterminated type name");
  368.  
  369.       if (p >= token_buffer + MAXTOKEN - 1)
  370.         fatals("type name too long (%d max)",MAXTOKEN-1);
  371.  
  372.       *p++ = c;
  373.       c = getc(finput);
  374.     }
  375.       *p = 0;
  376.       return (TYPENAME);
  377.         
  378.  
  379.     case '%':
  380.       return (parse_percent_token());
  381.  
  382.     default:
  383.       return (ILLEGAL);
  384.     }
  385. }
  386.  
  387.  
  388. /* parse a token which starts with %.  Assumes the % has already been read and discarded.  */
  389.  
  390. int
  391. parse_percent_token ()
  392. {
  393.   register int c;
  394.   register char *p;
  395.  
  396.   p = token_buffer;
  397.   c = getc(finput);
  398.  
  399.   switch (c)
  400.     {
  401.     case '%':
  402.       return (TWO_PERCENTS);
  403.  
  404.     case '{':
  405.       return (PERCENT_LEFT_CURLY);
  406.  
  407.     case '<':
  408.       return (LEFT);
  409.  
  410.     case '>':
  411.       return (RIGHT);
  412.  
  413.     case '2':
  414.       return (NONASSOC);
  415.  
  416.     case '0':
  417.       return (TOKEN);
  418.  
  419.     case '=':
  420.       return (PREC);
  421.     }
  422.   if (!isalpha(c))
  423.     return (ILLEGAL);
  424.  
  425.   while (isalpha(c) || c == '_')
  426.     {
  427.       if (p < token_buffer + MAXTOKEN)
  428.     *p++ = c;
  429.       c = getc(finput);
  430.     }
  431.  
  432.   ungetc(c, finput);
  433.  
  434.   *p = 0;
  435.  
  436.   if (strcmp(token_buffer, "token") == 0
  437.       ||
  438.       strcmp(token_buffer, "term") == 0)
  439.     return (TOKEN);
  440.   else if (strcmp(token_buffer, "nterm") == 0)
  441.     return (NTERM);
  442.   else if (strcmp(token_buffer, "type") == 0)
  443.     return (TYPE);
  444.   else if (strcmp(token_buffer, "guard") == 0)
  445.     return (GUARD);
  446.   else if (strcmp(token_buffer, "union") == 0)
  447.     return (UNION);
  448.   else if (strcmp(token_buffer, "expect") == 0)
  449.     return (EXPECT);
  450.   else if (strcmp(token_buffer, "start") == 0)
  451.     return (START);
  452.   else if (strcmp(token_buffer, "left") == 0)
  453.     return (LEFT);
  454.   else if (strcmp(token_buffer, "right") == 0)
  455.     return (RIGHT);
  456.   else if (strcmp(token_buffer, "nonassoc") == 0
  457.        ||
  458.        strcmp(token_buffer, "binary") == 0)
  459.     return (NONASSOC);
  460.   else if (strcmp(token_buffer, "semantic_parser") == 0)
  461.     return (SEMANTIC_PARSER);
  462.   else if (strcmp(token_buffer, "pure_parser") == 0)
  463.     return (PURE_PARSER);
  464.   else if (strcmp(token_buffer, "prec") == 0)
  465.     return (PREC);
  466.   else if (strcmp(token_buffer, "parser") == 0)
  467.     return (PARSER_NAME);
  468.   else return (ILLEGAL);
  469. }
  470.